
$(function () {
    $('.close-window').on('click', function () {
        $('#tip').hide()
    });
    $('.low-price').on('click', function () {
        $('#tip').show()
    });
    // 渲染猜你喜欢
    function renderGussYouLike (list) {
        for(var i = 0; i<list.length; i++) {
            if (list[i].carId == clientCommon.getUrlParam('carId')) {
                list.splice(i,1)
            }
            list[i].carLicenceDate = clientCommon.handleDate('yyyy-MM',list[i].carLicenceDate);
            $('#guss-list').append('<a href="' + '../../html/list/list-detail.html?carId=' + list[i].carId +'" class="car-item"><div class="car-item-box">\n' +
                '<img src="' + imageUrl + list[i].carImageUrl + '.r_290xh_230' + '">\n' +
                '<p class="car-info item-common ">' + list[i].carTitle + '</p>\n' +
                '<p class="car-sub-info item-common">' + list[i].carLicenceDate + "上牌" + list[i].carMileage + "万公里" + '</p>\n' +
                '<p class="car-item-price item-common"><span>￥</span>' + list[i].carRetailPrice + "万元" +'</p>\n' +
                '</div></a>');
        }
        $('.list-content-box').hide();
        $('.list-content-box').height($('.list-content-box').height());
        $('.list-content-box').show();
    };
    
    // 渲染配置参数
    function renderConfiger (list) {
        //渲染配置
        var confArr = [];
        for (var i = 0; i < list.length; i++) {
            for (var j = 0; j < list[i].childCarConf.length; j++) {
                if (list[i].childCarConf[j].selectedState) {
                    var html = '<td><span>'+list[i].childCarConf[j].confName+'</span><span>●</span></td>'
                    confArr.push(html);
                }
            }
        }
        for(var i=0;i<confArr.length;i+=4){
            var html = confArr.slice(i,i+4).join('');
            $('#config-table').append('<tr>'+html+'</tr>');
        }
    };
    // 询问低价
    function askLowPrice() {
        $('.low-price').click(function () {
            if (clientCommon.getLocal('_loginInfo') == null) {
                $('#tip').show();
                $('#loginBox').show();
            } else {
                $('#tip').show();
                $('#askLowPrice').show();
            }
        });
        // 验证询问低价手机号
        $('#asklowprice-tel').on('input', function () {
            lowPriceBtn()
        });
    };
    // 点击询问低价
    function lowPriceBtn() {
        if (clientCommon.isPhone($('#asklowprice-tel').val())) {
            $('#low-price').css({'background': '#FF6C00'});
        }else{
            $('#low-price').css({'background': '#ccc'});
        }
        $('#low-price').on('click', function () {
            if(clientCommon.isPhone($('#asklowprice-tel').val())){
                $.ajax({
                    url:$apiAddress.getRecordIntentionInfo,
                    data:{carId:clientCommon.getUrlParam('carId'),intentionMobile:$('#asklowprice-tel').val(), userMobile:clientCommon.getLocal('_loginInfo').userMobile},
                    type: "get",
                    dataType:'json',
                    success: function (res) {
                        if (res.code == 1) {
                            layer.msg('信息提交成功，车主将尽快与您取得联系！', {
                                time: 1000,
                            });
                            $('#tip').hide();
                            $('#askLowPrice').hide();
                        } else {
                            layer.msg(res.msg, {
                                time: 1000,
                            });
                            return false
                        }
                    }
                });
            }else{
                return false
            }
        })
    }
    // 渲染详情页面
    function renderCarDetail(list) {
        if(list.carAuthState == 0){
            $('#QYRZ').hide()
        }else{
            $('#QYRZ').show()
        }
        var empty = '/'
         // 渲染首图
        for (var i = 0; i < list.carImages.length; i++) {
            var imgHtml = '<div class="swiper-slide"><img src="'+ imageUrl + list.carImages[i].imageUrl +'.r_500xh_320"></div>'
            $('#swiperWrapper').append(imgHtml)
        }
        var mySwiper = new Swiper('.swiper-container', {
            autoplay: true,//可选选项，自动滑动
            // 如果需要分页器
            pagination: {
              el: '.swiper-pagination',
              clickable :true,
            },
            loop : true,
            // 如果需要前进后退按钮
            navigation: {
              nextEl: '.swiper-button-next',
              prevEl: '.swiper-button-prev',
            },
        })
        $('.item-right-box').append('<h3>' + list.carTitle + '</h3>\n');
        if (list.carAuthState == 10) {
            $('.item-right-box').append('<p class="QY-validate"><img src="../../images/认证icon04@2x.png"></img></p>')
        }
        if(!list.carRetailPrice ||list.carRetailPrice == 0){
            list.carRetailPrice = '面议';
            p = '<p class="price">面议</p>';
            $('.item-right-box').append(p)
        }else{
            p = '<p class="price"><span>￥</span>' + list.carRetailPrice + '万元</p>';
            $('.item-right-box').append(p)
        }
        if (list.carLicenceState == 1) {
            $('.item-right-box').append('<p class="car-item-detail"><span><a>' + clientCommon.handleDate('yyyy-MM-dd' ,list.carLicenceDate) + '</a><a class="a-bottom">上牌</a></span></p>')
        } else if(list.carLicenceState == 3) {
            $('.item-right-box').append('<p class="car-item-detail"><span><a>新车</a><a class="a-bottom">--</a></span></p>')
        }else{
            $('.item-right-box').append('<p class="car-item-detail"><span><a>未上牌</a><a class="a-bottom">--</a></span></p>')
        }
        $('.car-item-detail').append('<img class="line" src="../../images/line.png">\n' 
        + '<span class="span-margin-left"><a>' + list.carRadarInfo.carMileage.toFixed(2) + '万公里' + '</a><a class="a-bottom">表显里程</a></span>\n'
        + '<img class="line" src="../../images/line.png">\n'
        +'<span class="span-margin-left"><a>' + list.carGearbox +'</a><a class="a-bottom">变速箱</a></span>\n'
        +'<img class="line" src="../../images/line.png">\n'
        +'<span class="span-margin-left"><a>' + list.market.city.cityName +'</a><a class="a-bottom">所在城市</a></span>\n'
        +'<img class="line" src="../../images/line.png">\n'
        +'<span class="span-margin-left"><a>' + list.carRadarInfo.carTransferNum +'</a><a class="a-bottom">过户次数</a></span>\n'
        +'<img class="line" src="../../images/line.png">\n'+'<span class="span-margin-left"><a>' + list.carRadarInfo.carDisplacement +'</a><a class="a-bottom">排量</a></span>\n');
        $('.item-right-box').append('<p class="low-price">询问底价</p> ');
        askLowPrice();

        // 渲染基本信息
        if (list.carTransferNum > 0) {
            if (list.carAuthState == 10) {
                twoCar('已认证','不是', list)
            }else{
                twoCar('未认证','不是', list)
           }
        } else {
            if (list.carAuthState == 10) {
                twoCar('已认证','是', list)
            }else{
                twoCar('未认证','是', list)
           }
        }

        // 渲染配置参数
        // 车身类型
        $('#config-one').append('<tr class="table-header"><th colspan="4"><span>车身类型</span></th></tr>\n'
        +'<tr><td><span>车身类型</span><span>'+list.carType.carTypeName+'</span></td><td><span>车身结构</span><span>'+list.model.modelBodywork+'</span></td><td><span>长度</span><span>'+list.model.modelLength+'</span></td><td><span>宽度</span><span>'+list.model.modelWidth+'</span></td></tr>\n'
        +'<tr><td><span>高度(mm)</span><span>'+list.model.modelHeight+'</span></td><td><span>前后轮距(mm)</span><span>'+list.model.modelWheelbase+'</span></td><td></td><td></td></tr>\n')
        // 发动机参数
        $('#engine').append('<tr class="table-header"><th colspan="4"><span>发动机参数</span></th></tr>\n'
        +'<tr><td><span>发动机型号</span><span>'+list.model.modelEngineModel+'</span></td><td><span>进气形式</span><span>'+list.model.modelInletModel+'</span></td><td><span>气缸排列形式</span><span>'+list.model.modelCylinderArray+'</span></td><td><span>气缸个数</span><span>'+list.model.modelCylinderNum+'</span></td></tr>\n'
        +'<tr><td><span>排气量</span><span>'+list.model.modelDisplacement+'</span></td><td><span>燃料形式</span><span>'+list.model.modelFuelRoz+'</span></td><td><span>排放标准</span><span>'+list.carEnviron +'</span></td><td></td></tr>\n')
        // 猜你喜欢
        _getBaseDate.gussYouLike(renderGussYouLike, {
            carRetailPrice: list.carRetailPrice,
            marketId: 1,
            pageIndex: 1,
            pageSize: 8
        });

        // 查询车辆配置
         _getBaseDate.getCarConfById(renderConfiger, {carId: list.carId})

        getCarConfByModelId(list.modelId);
        // 渲染车辆图片
        for(var i = 0; i< list.carImages.length; i++) {
            $('#carImages').append('<div class="car-image-box"><img src="' + imageUrl + list.carImages[i].imageUrl + '.r_580xh_390'+ '"><p>'+clientCommon.getImgType(list.carImages[i].imageKey)+'</p></div>');
        }
    };
    // 基本信息参数
     function twoCar ( autoCar, judge, list) {
        $('#baseinfo').append('<tr><td><span>车身类型</span><span>'+list.carType.carTypeName+'</span></td><td><span>首次上牌</span><span>'+clientCommon.handleDate('yyyy-MM',list.carLicenceDate)   +'</span></td> <td><span>是否一手车</span><span>'+judge+'</span></td><td><span>登记证</span><span>--</span></td></tr>\n'
        +'<tr><td><span>变速箱</span><span>'+list.carGearbox+'</span></td><td><span>行驶里程</span><span>'+list.carRadarInfo.carMileage+'</span></td> <td><span>交强险到期日</span><span>'+ clientCommon.handleDate('yyyy-MM',list.carForceInsuranceDate) +'</span></td><td><span>原始购车发票</span><span>--</span></td></tr>\n'
        +'<tr><td><span>生产厂家</span><span>'+list.model.modelManufacturer+'</span></td><td><span>车身颜色</span><span>'+list.carOutColor+'</span></td> <td><span>商业险到期日期</span><span>'+clientCommon.handleDate('yyyy-MM',list.carBusinessInsuranceDate)+'</span></td><td><span>是否认证</span><span>'+autoCar+'</span></td></tr>\n'
        +'<tr><td><span>品牌车系</span><span>'+list.brand.brandName+''+list.series.seriesName+'</span></td><td><span>内饰颜色</span><span>'+list.carInColor+'</span></td> <td><span>车架号(vin码)</span><span>'+list.carVin+'</span></td><td><span> 车辆所在地</span><span>'+list.region.regionName+'</span></td></tr>\n'
        +'<tr><td><span>保养记录</span><span>'+(list.car4sUpkeepState==1?'有':'无')+'</span></td><td><span>使用性质</span><span>'+clientCommon.getCarPurpose(list.carPurpose)+'</span></td> <td><span>行驶证</span><span>--</span></td><td><span>排放标准</span><span>'+list.carEnviron+'</span></td></tr>\n');
     }
    _getBaseDate.getCarById(renderCarDetail, {
        carId: clientCommon.getUrlParam('carId'),
        imageViewId: 3
    });
    function getCarConfByModelId (modelid) {
        $.ajax({
            url:$apiAddress.getCarConfByModelId,
            data:{modelId: modelid},
            type: "get",
            dataType:'json',
            success: function (res) {
                console.log(res.result)
                if (res.code == 1) {
                    var brightNum = 0;
                    for(var i = 0; i< res.result.length; i++) {
                        for( var j = 0; j < res.result[i].childCarConf.length; j++) {
                            if (res.result[i].childCarConf[j].confBrightSpot == 1&&res.result[i].childCarConf[j].selectedState) {
                                brightNum ++;
                                if (brightNum == 0) {
                                    $('#img-box').css({'height': 0});
                                } else if (brightNum>8) {
                                    $('#img-box').css({'height': 250});
                                } else {
                                    $('#img-box').css({'height': 250/2});
                                }
                                renderBriling(res.result[i].childCarConf[j].confName);
                            }
                        }
                    }
                } else {
                    layer.msg(res.msg, {
                        time: 1000,
                    });
                    return false
                }
            }
        });
    };
    function renderBriling(tit) {
        $('#img-box').append('<div class="img-item"><img src="'+clientCommon.brightImg(tit)+'"><p>'+tit+'</p ></div>')
    }
});